const app = getApp()
import Toast from '@vant/weapp/toast/toast';
import {
  reqUpdateUser,
} from '../../../request/api.js'
import {
  checkRegNo,
} from '../../../utils/validate.js'
Page({
  data: {
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    srcDefaultAvatar1: '../../../statics/images/personal-center/touxiang1.png',
    srcDefaultAvatar2: '../../../statics/images/personal-center/touxiang2.png',
    srcDefaultAvatar3: '../../../statics/images/personal-center/touxiang3.png',
    srcDefaultAvatar4: '../../../statics/images/personal-center/touxiang4.png',
    srcDefaultAvatar5: '../../../statics/images/personal-center/touxiang5.png',
    srcDefaultAvatar6: '../../../statics/images/personal-center/touxiang6.png',
    srcDefaultAvatar: '../../../statics/images/personal-center/default_avatar.png',
    tenantId: '',
    username: '',
    mobile: '未绑定',
    companyName: '',
    rolename: '',
    nickname: '',
    idCard: "",
    nicknameTemp: '',
    avatar: '1',
    imgavatar: '1',
    isEdit: false, //是否可编辑
    disabledBtn: false,
    showSpace: false,
    isProprietor: false, //装维||业主
  },
  onLoad(options) {
    this.setData({
      tenantId: app.globalData.tenantId,
      username: app.globalData.username,
      mobile: app.globalData.mobile || '未绑定',
      companyName: app.globalData.companyName,
      rolename: app.globalData.rolename,
      descriptionRole: app.globalData.descriptionRole,
      isProprietor: app.globalData.isProprietor
    })
    if (options.path) {
      this.cutImg(options.path)
    }
  },
  onShow() {
    this.setData({
      nickname: app.globalData.nickname,
      idCard: app.globalData.idCard,
      nicknameTemp: app.globalData.nickname,
      avatar: app.globalData.avatar || wx.getStorageSync('avatar'),
    })
  },
  //上传图片
  // chooseImage() {
  //     wx.chooseImage({
  //         count: 1,
  //         sizeType: ['compressed'],
  //         sourceType: ['album', 'camera'],
  //         success(res) {
  //             const tempFilePaths = res.tempFilePaths
  //             if (res.tempFiles[0].size / 1024 / 1024 < 0.5) {
  //                 wx.redirectTo({
  //                     url: '/pages/cropper/cropper?tempFilePaths=' + tempFilePaths
  //                 })
  //             } else {
  //                 showMsg('您上传的图片不能大于500kb');
  //             }
  //         }
  //     })
  // },
  //裁剪
  cutImg(tempFilePaths) {
    var _this = this
    //   文件上传  接口
    wx.uploadFile({
      url: app.globalData.reqUrl + 'file/upload',
      filePath: tempFilePaths,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data"
      },
      formData: {
        "token": app.globalData.token
      },
      success: function (res) {
        var res = JSON.parse(res.data)
        if (res.code == 20000) {
          _this.setData({
            // avatar: res.data.url,
            showBtn: true
          })
        } else if (res.statusCode != 20000) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }
      },
      fail: function (e) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast(); //隐藏Toast
      }
    })
  },
  // 获取昵称
  nicknameChange(e) {
    this.setData({
      nickname: e.detail.value,
    })
  },
  // 获取身份证
  idCardChange(e) {
    this.setData({
      idCard: e.detail.value,
    })
  },
  // 确定修改用户信息
  async editUserInfoConfirm() {
    if (this.data.disabledBtn) return
    let {
      nickname,
      nicknameTemp,
      // avatar,
      isEdit,
      username,
      idCard
    } = this.data
    if (!isEdit) {
      this.setData({
        isEdit: true
      })
      return
    }
    if (!checkRegNo(idCard)) return
    // if (nicknameTemp === nickname) return Toast("昵称未做出修改");
    let params = {
      id: app.globalData.userId,
      // avatar,
      nickname,
      username,
      idCard
    }
    const res = await reqUpdateUser(params)
    if (res.code != 20000) return Toast.fail(res.message);
    Toast.success("修改成功")
    wx.setStorageSync('nickname', nickname);
    app.globalData.nickname = nickname
    app.globalData.idCard = idCard
    wx.setStorageSync('idCard', idCard);
    // wx.setStorageSync('avatar', avatar);
    // app.globalData.avatar = avatar
    this.setData({
      disabledBtn: true
    })
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
  },
  // 关闭弹出层
  closeSpace() {
    this.setData({
      showSpace: false,
      // avatar:'1',
      // imgavatar:'1'
    })
  },
  // 点击头像
  chooseImage(e) {
    console.log(e.currentTarget.dataset.value)
    this.setData({
      showSpace: true,
      imgavatar: e.currentTarget.dataset.value || '1'
    })
  },
  // 修改头像
  async confirmSpace() {
    let {
      nickname,
      imgavatar,
      username,
      idCard
    } = this.data
    let params = {
      id: app.globalData.userId,
      avatar: imgavatar,
      nickname,
      username,
      idCard
    }
    const res = await reqUpdateUser(params)
    if (res.code != 20000) return Toast.fail(res.message);
    Toast.success("修改成功")
    wx.setStorageSync('avatar', imgavatar);
    app.globalData.avatar = imgavatar;
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
  }

})